const jsonServer = require('json-server')
const JWT = require('jsonwebtoken')
const Mock = require('mockjs')
const { createProxyMiddleware } = require('http-proxy-middleware')
const server = jsonServer.create()

server.use(jsonServer.bodyParser)

let { list } = Mock.mock({
  'list|100': [
    {
      id: '@id',
      name: '@cname',
      'sex|+1': ['男', '女'],
      'age|10-20': 10,
      birthday: '@datetime'
    }
  ]
})

// const middleware = jsonServer.defaults()

// server.use(middleware)
// server.use('/api/login', createProxyMiddleware({
//   target: 'http://localhost:8080', // 后台服务器地址
//   changeOrigin: true
// pathRewrite: {
// '^/httpServer': 'http://localhost:3000' // 本地地址
// }
// }))

const users = [
  {
    username: 'admin',
    password: '123456',
    role: 'admin'
  }, {
    username: 'user',
    password: '123456',
    role: 'user'
  }
]

const router = {
  user: [
    {
      path: '/home',
      name: 'home',
      meta: {
        title: 'HOME',
        icon: 'home-outlined'
      }
    }, {
      path: '/list',
      name: 'list',
      meta: {
        title: 'LIST',
        icon: 'menu-outlined'
      },
      children: [
        {
          path: '/list/edit',
          name: 'edit',
          meta: {
            title: 'EDIT',
            icon: 'menu-outlined'
          }
        }
      ]
    }
  ],
  admin: [
    {
      path: '/home',
      name: 'home',
      meta: {
        title: 'HOME',
        icon: 'home-outlined'
      }
    }, {
      path: '/list',
      name: 'list',
      meta: {
        title: 'LIST',
        icon: 'menu-outlined'
      },
      children: [
        {
          path: '/list/edit',
          name: 'edit',
          meta: {
            title: 'EDIT',
            icon: 'menu-outlined'
          }
        }
      ]
    }, {
      path: '/set',
      name: 'set',
      meta: {
        title: 'SET',
        icon: 'setting-outlined'
      }
    }
  ]
}

server.post('/api/login', (req, res) => {
  const { username, password } = req.body
  const user = users.find(item => item.username === username && item.password === password)
  if (user) {
    res.send({
      code: 200,
      msg: '登录成功',
      username: user.username,
      role: user.role,
      token: JWT.sign({
        username: user.username,
        role: user.role
      }, '666')
    })
  } else {
    res.send({
      code: 400,
      msg: '登录失败'
    })
  }
})

server.get('/api/router', (req, res) => {
  console.log(req)
  const { authorization: token } = req.headers

  const { role } = JWT.verify(token, '666')
  res.send({
    code: 200,
    router: router[role]
  })
})

server.get('/api/getList', (req, res) => {
  const { page, size, limitAge } = req.query
  const realList = limitAge ? list.filter(item => item.age < limitAge) : list
  res.send({
    code: 200,
    total: realList.length,
    list: realList.slice((page - 1) * size, page * size)
  })
})

server.post('/api/addItem', (req, res) => {
  list.unshift({
    ...req.body,
    id: new Date().getTime()
  })
  res.send({
    code: 200
  })
})

server.get('/api/deleteItem', (req, res) => {
  const { id } = req.query
  list = list.filter(item => item.id != id)
  res.send({
    code: 200
  })
})

server.post('/api/editItem', (req, res) => {
  const { id } = req.body
  const index = list.findIndex(item => item.id == id)
  if (index > -1) {
    list[index] = req.body
  }
  res.send({
    code: 200
  })
})

server.listen('3000', () => {
  console.log('json server running')
})
